/*
 * 文件名: MonthDayLinearLayout.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XUJIAO001
 * 创建时间: 2012-1-18
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.calendar.dialog;

import java.util.Calendar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import app.utils.CommonUtils;
import app.widget.calendar.widget.OnWheelChangedListener;
import app.widget.calendar.widget.OnWheelScrollListener;
import app.widget.calendar.widget.WheelView;
import app.widget.calendar.widget.adapter.DateArrayAdapter;

import com.longevityassistant.activity.R;

/**
 * @author EX-XUJIAO001
 * @date 2012-1-18
 * @version [Android PABank C01, @2012-1-18]
 * @description 月日 自定义layout
 */
public class MonthDayLinearLayout extends LinearLayout implements
		OnWheelChangedListener, OnWheelScrollListener {
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 月
	 */
	private WheelView month;
	/**
	 * 日
	 */
	private WheelView day;
	/**
	 * 月
	 */
	private int mMonth;
	/**
	 * 日
	 */
	private int mDay;
	/**
	 * 是否第一次onLayout过
	 */
	private boolean isOnLayout;
	/**
	 * 获取后台服务时间
	 */
	private Calendar calendar;

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 */
	public MonthDayLinearLayout(Context context) {
		super(context);
		this.context = context;
		init();
	}

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 */
	public MonthDayLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化操作
	 */
	private void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View root = inflater.inflate(R.layout.app_widget_month_day_layout, null);
		root.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		addView(root);

		//Calendar calendar = Calendar.getInstance();
		calendar = CommonUtils.getCurrentTimeByCalendar();
		int curMonth = calendar.get(Calendar.MONTH);
		int curDay = calendar.get(Calendar.DAY_OF_MONTH);
		mMonth = curMonth + 1;
		mDay = curDay;
		month = (WheelView) root.findViewById(R.id.month);
		month.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.MONTHS, curMonth));
		month.setCurrentItem(curMonth);
		month.addChangingListener(this);
		month.addScrollingListener(this);

		day = (WheelView) root.findViewById(R.id.day);
		CalendarDialogCommon.updateDays(context, month, day,calendar);
		day.setCurrentItem(curDay - 1);
		day.addChangingListener(this);
		day.addScrollingListener(this);
	}

	@Override
	public void onScrollingStarted(WheelView wheel) {
		if (onMonthDayScrollListener != null) {
			onMonthDayScrollListener.onMonthDayScrollEnd(true);
		}

	}

	@Override
	public void onScrollingFinished(WheelView wheel) {
		if (onMonthDayScrollListener != null) {
			onMonthDayScrollListener.onMonthDayScrollEnd(false);
		}

	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		switch (wheel.getId()) {
		case R.id.month:
			mMonth = newValue + 1;
			CalendarDialogCommon.updateDays(context, month, day,calendar);
			break;
		case R.id.day:
			mDay = newValue + 1;
			break;
		default:
			break;
		}
		if (onMonthDayScrollListener != null) {
			onMonthDayScrollListener.onMonthDayScrollChange(mMonth, mDay);
		}

	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @author EX-XUJIAO001
	 * @version [Android PABank C01, 2012-1-18]
	 */
	public interface OnMonthDayScrollListener {
		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述]
		 * 
		 * @param month
		 *            月
		 * @param day
		 *            日
		 */
		void onMonthDayScrollChange(int month, int day);

		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述]
		 * 
		 * @param scrollState
		 *            滚动的状态
		 */
		void onMonthDayScrollEnd(boolean scrollState);
	}

	/**
	 * 回调监听
	 */
	private OnMonthDayScrollListener onMonthDayScrollListener;

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] setter
	 * 
	 * @param onMonthDayScrollListener
	 *            回调监听
	 */
	public void setOnMonthDayScrollListener(
			OnMonthDayScrollListener onMonthDayScrollListener) {
		this.onMonthDayScrollListener = onMonthDayScrollListener;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] getter
	 * 
	 * @return OnMonthDayScrollListener
	 */
	public OnMonthDayScrollListener getOnMonthDayScrollListener() {
		return onMonthDayScrollListener;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		/**
		 * 监听回调
		 */
		if (!isOnLayout) {
			if (onMonthDayScrollListener != null) {
				onMonthDayScrollListener.onMonthDayScrollChange(mMonth, mDay);
			}
			isOnLayout = true;
		}

	}

}
